/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2023-12-22 09:20:34
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2023-12-22 09:36:54
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/device_detail/base_detail/base_detail_controller.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:async';

import 'package:get/get.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/socket/use_socket.dart';
import 'package:yt_devops/utils/socket/use_subscribe.dart';

class BaseDetailController extends GetxController {
  UseSubsribe connectSubscribe = UseSubsribe();

  Stream<Map<String, dynamic>>? stream;

  ConnectStatusType? connectStatus;

  StreamSubscription<Map<String, dynamic>>? connectStatusStreamSubscription;

  @override
  void onInit() {
    var deviceId = Get.arguments['deviceId'];
    stream = connectSubscribe
        .start([deviceId], true, type: MessageEventType.networkStatus).stream;
    connectStatusStreamSubscription = stream?.listen((data) {
      connectStatus = $enumDecodeNullable(
          $ConnectStatusTypeEnumMap, data['networkStatus'],
          unknownValue: ConnectStatusType.unConfig);
      update();
    });
    super.onInit();
  }

  @override
  void onClose() {
    connectStatusStreamSubscription?.cancel();
    connectSubscribe.closeListen();

    super.onClose();
  }
}
